package com.example.mall.dao;

import com.example.mall.model.Assemble;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

@Repository
public class AssembleDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void insert(Assemble assemble) {
        String sql = "insert into assemble (timeStart, timeEnd, cID) values (?, ?, ?)";
        jdbcTemplate.update(sql, assemble.getTimestart(), assemble.getTimeend(), assemble.getcID());
    }

    public void delete(Integer id) {
        String sql = "delete from assemble where id = ?";
        jdbcTemplate.update(sql, id);
    }

    public <E> void update(Integer id, E new_set, String choice) {
        String sql = "update assemble set " + choice + " = ? where id = ?";
        jdbcTemplate.update(sql, new_set, id);
    }

    public Assemble getById(Integer id) {
        String sql = "select * from assemble where id = ? lock in share mode";
        RowMapper<Assemble> rowMapper = new BeanPropertyRowMapper<>(Assemble.class);
        return jdbcTemplate.queryForObject(sql, rowMapper, id);
    }

}
